package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.model.vx.vo.WxAddressDetailRespVO;
import com.cskaoyan.model.vx.vo.WxAddressListRespVO;
import com.cskaoyan.service.wx.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @ClassName: WxAddressController
 * @Author: WangEnzhao
 * @Description: 微信地址管理
 * @CreateTime: 2021/12/11:23:00
 */
@RequestMapping("wx/address")
@RestController
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

    /***
     * @Author: WangEnzhao
     * @Description: 用来查询微信地址
     * @CreateTime: 11:53 12/2/2021
     * @Param: [session]
     * @return: com.cskaoyan.model.admin.vo.BaseRespVo
     */
    @RequestMapping("list")
    public BaseRespVo addressList(HttpSession session) {
        // 获取session中的userId
        Integer userId = (Integer) session.getAttribute("wxUserId");
        if(userId == null){
            return BaseRespVo.fail(404,"userId不能获取");
        }
        WxAddressListRespVO wxAddressListRespVO = wxAddressService.addressList(userId);

        return BaseRespVo.ok(wxAddressListRespVO);
    }

    /***
    * @Author: WangEnzhao
    * @Description: 插入地址或者更新地址
    * @CreateTime: 17:02 12/2/2021
    * @Param: [session, marketAddress]
    * @return: com.cskaoyan.model.admin.vo.BaseRespVo
    */
    @RequestMapping("save")
    public BaseRespVo addressSave(HttpSession session, @RequestBody MarketAddress marketAddress) {

        //判断参数是否正确
        boolean matches = marketAddress.getTel().matches("^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$");
        if (matches == false) {
            return BaseRespVo.fail(401, "手机号码格式不正确");
        }

        //使用session获取
        Integer userId = (Integer) session.getAttribute("wxUserId");
        if(userId == null){
            return BaseRespVo.fail(404,"userId不能获取");
        }
        marketAddress.setUserId(userId);


        if(marketAddress.getId() == 0){
            //如果id为0，执行插入
            Integer id = wxAddressService.addressSave(marketAddress);
            return BaseRespVo.ok(id);
        }

        //如果id不是0，执行更新
        Integer id = wxAddressService.addressUpdate(marketAddress);
        return BaseRespVo.ok(id);

    }

    /***
     * @Author: WangEnzhao
     * @Description: 删除自己的地址
     * @CreateTime: 14:50 12/2/2021
     * @Param: [id, session]
     * @return: com.cskaoyan.model.admin.vo.BaseRespVo
     */
    @RequestMapping("delete")
    public BaseRespVo addressDelete(@RequestBody Map<String,Integer> map, HttpSession session) {
        // 获取session中userId
        Integer userId = (Integer) session.getAttribute("wxUserId");
        if(userId == null){
            return BaseRespVo.fail(404,"userId不能获取");
        }

        Integer code = wxAddressService.addressDelete(map.get("id"), userId);
        if(code == 1){
            return BaseRespVo.ok(null);
        }
        return BaseRespVo.fail(400,"非法删除");
    }

    /***
    * @Author: WangEnzhao
    * @Description: 回显某个地址
    * @CreateTime: 15:21 12/2/2021
    * @Param: []
    * @return: com.cskaoyan.model.admin.vo.BaseRespVo
    */
    @RequestMapping("detail")
    public BaseRespVo addressDetail(HttpSession session, Integer id){
        // 获取session中userId
        Integer userId = (Integer) session.getAttribute("wxUserId");
        if(userId == null){
            return BaseRespVo.fail(404,"userId不能获取");
        }
        MarketAddress marketAddress = wxAddressService.addressDetail(id, userId);
        return BaseRespVo.ok(marketAddress);
    }

}
